﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RxxAdmin.Common.Helper;
using RxxAdmin.Common.HttpContextUser;
using RxxAdmin.IServices;
using RxxAdmin.Model;
using RxxAdmin.Model.Entity;
using RxxAdmin.Model.ViewModels;
using RxxAdmin.Services;

namespace RxxAdmin.Api.Controllers.permission;
/// <summary>
/// 菜单管理
/// </summary>
public class SysMenuController : BaseApiController
{
    private readonly ISysMenuService _sysMenuService;
    private readonly IMapper _mapper;

    public SysMenuController(ISysMenuService sysMenuService, IMapper mapper)
    {
        _sysMenuService = sysMenuService;
        _mapper = mapper;
    }
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <returns></returns> 
    [HttpGet]
    public async Task<MessageModel<List<SysMenu>>> MenuTreeList([FromQuery] SysMenuIn input)
    {
        return Success(await _sysMenuService.MenuTreeList(input));
    }

    /// <summary>
    /// 添加菜单
    /// </summary>
    /// <returns></returns> 
    [HttpPost]
    public async Task<MessageModel<string>> Add(SysMenu input)
    {
        var id = await _sysMenuService.Add(input);
        return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed("添加失败");
    }

    /// <summary>
    /// 删除菜单
    /// </summary>
    /// <returns></returns> 
    [HttpDelete("{id}")]
    public async Task<MessageModel<string>> Delete(int id)
    {
        if (id <= 0) return Failed();
        var userDetail = await _sysMenuService.QueryById(id);
        if (userDetail == null) return Success<string>(null, "角色不存在");
        return await _sysMenuService.Delete(userDetail) ? Success(userDetail?.Id.ObjToString(), "删除成功") : Failed();
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    /// <returns></returns> 
    [HttpPut]
    public async Task<MessageModel<string>> Update(SysMenu input)
    {
        if (input == null || input.Id <= 0)
            return Failed("缺少参数");

        return await _sysMenuService.Update(input) ? Success(input?.Id.ObjToString(), "更新成功") : Failed("更新失败");
    }
}
